package com.jhz.speedy.common.util;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.widget.TextView;

import com.jhz.speedy.R;


/**
 * 对话框工具
 * Created by JunHeZ on 2017/6/30.
 */
public class DialogUtils {
    private Dialog mDialog;
    private static DialogUtils dialogUtils;
    public static final int THEME_WHITE = 1;
    public static final int THEME_BLACK = 0;

    private DialogUtils() {
    }

    public static DialogUtils getInstance() {
        if (dialogUtils == null) {
            dialogUtils = new DialogUtils();
        }
        return dialogUtils;
    }

    public void dismisDialog() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    /**
     * 加载对话框，默认的白色底主题
     * @param textStr 提示语
     */
    public void showLoadDialog(final Activity activity, String textStr){
        showLoadDialog(activity, true, THEME_WHITE, textStr, 0);
    }

    /**
     * 加载对话框，默认的白色底主题
     * @param resStr 提示语资源id
     */
    public void showLoadDialog(final Activity activity, int resStr){
        showLoadDialog(activity, true, THEME_WHITE, null, resStr);
    }

    /**
     * @param canCanceled 是否允许用户取消
     * @param theme  类型，0黑色， 1白色
     * @param textStr 提示语
     * @param resStr 提示语资源id
     */
    public void showLoadDialog(final Activity activity, boolean canCanceled, int theme, String textStr, int resStr){
        dismisDialog();
        mDialog = new Dialog(activity, R.style.dialog_transparent_style);
        if(theme == THEME_WHITE){
            mDialog.setContentView(R.layout.dialog_load_white);
        }else {
            mDialog.setContentView(R.layout.dialog_load_black);
        }
        mDialog.getWindow().getAttributes().width = (int) (ScreenSizeUtils.getScreenWidth() * 0.7f);
        mDialog.setCanceledOnTouchOutside(canCanceled);
        mDialog.setCancelable(canCanceled);

//        ProgressBar loadProgress = (ProgressBar) mDialog.findViewById(R.id.loadProgress);
        TextView tvLoad = (TextView) mDialog.findViewById(R.id.tvLoad);
        if(resStr != 0){
             tvLoad.setText(resStr);
        }else {
            if(! TextUtils.isEmpty(textStr))
                tvLoad.setText(textStr);
        }


        mDialog.show();
    }

}
